//
//  JoinTeamViewController.m
//  MeterMart
//
//  Created by ENERGY on 2018/9/28.
//  Copyright © 2018年 ENERGY. All rights reserved.
//

#import "JoinTeamViewController.h"
#import "JoinTeamTableViewCell.h"
#import "ShareView.h"
@interface JoinTeamViewController ()<UITableViewDelegate, UITableViewDataSource>
@property (nonatomic, strong)UIImageView * codeImg;//二维码
@property (nonatomic, strong)UITableView * tableView;
@property (nonatomic, strong)NSMutableDictionary * cellDic;
@property (nonatomic, strong)ShareView * shareView;
@end

@implementation JoinTeamViewController(networkin)
- (void)getCodeImgData{
    NSDictionary * dic = @{
                           @"unionid":[UserModel sharedInstanced].userID
                           };
    [self showprogressHUD];
    [[AFAppDotNetAPIClient sharedClient] POST:@"usApi/joinTeam" parameters:dic progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        [self hiddenProgressHUD];
        if ([responseObject[@"sign"] isEqualToString:@"1"]) {
            [self.cellDic addEntriesFromDictionary:responseObject[@"list"]];
            [self.tableView reloadData];
        }else{
            ShowMessage(responseObject[@"msg"]);
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        [self hiddenProgressHUD];
        [self showError:error];
    }];
}
@end
@implementation JoinTeamViewController
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:NO animated:NO];
    
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self getCodeImgData];//网络接口
    self.view.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.tableView];
    //分享按钮
    UIButton * shareBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 25/WIDTH_6_SCALE, 25/WIDTH_6_SCALE)];
    [shareBtn setImage:[UIImage imageNamed:@"share"] forState:UIControlStateNormal];
    [shareBtn addTarget:self action:@selector(shareBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem * rightButtonItem = [[UIBarButtonItem alloc] initWithCustomView:shareBtn];
    self.navigationItem.rightBarButtonItem = rightButtonItem;
    if (![WXApi isWXAppInstalled]) {
        shareBtn.hidden = YES;
    }
}
- (ShareView *)shareView{
    if (!_shareView) {
        __weak typeof (self)weakSelf = self;
        _shareView = [[ShareView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - BOTTOM_HEIGHT)];
        _shareView.friendBtnClickBlock = ^{
            [weakSelf wxShare:0];
        };
        _shareView.circleBtnClickBlock = ^{
            [weakSelf wxShare:1];
        };
    }return _shareView;
}
- (NSMutableDictionary *)cellDic{
    if (!_cellDic) {
        _cellDic = @{}.mutableCopy;
    }return _cellDic;
}

- (UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - BOTTOM_HEIGHT - NAVI_HEIGHT) style:UITableViewStylePlain];
        _tableView.backgroundColor = [UIColor whiteColor];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.rowHeight = SCREEN_HEIGHT - BOTTOM_HEIGHT - NAVI_HEIGHT;
        [_tableView registerClass:[JoinTeamTableViewCell class] forCellReuseIdentifier:NSStringFromClass([JoinTeamTableViewCell class])];
    }return _tableView;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    JoinTeamTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([JoinTeamTableViewCell class]) forIndexPath:indexPath];
    cell.model = self.cellDic;
    return cell;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}
#pragma mark - action
- (void)shareBtnClick:(UIButton *)sender{
    [self.shareView showView];
}
//分享
- (void)wxShare:(int)scene{
    NSLog(@"微信分享");
    if ([WXApi isWXAppInstalled] && [WXApi isWXAppSupportApi]) {
        [self weChatShareURL:scene];
    }else {
        // 提示用户安装微信
        ShowMessage(@"请安装微信");
    }
}
//分享
- (void)weChatShareURL:(int)scene{
    WXMediaMessage *message = [WXMediaMessage message];
    // 设置消息缩略图的方法
    [message setThumbImage:[UIImage imageNamed:@"martLogo"]];
    // 多媒体消息中包含的图片数据对象
    WXImageObject *imageObject = [WXImageObject object];
    JoinTeamTableViewCell * cell = (JoinTeamTableViewCell *)[self.tableView dequeueReusableCellWithIdentifier:NSStringFromClass([JoinTeamTableViewCell class])];
    cell.model = self.cellDic;
    UIImage *image = cell.img.image;
    // 图片真实数据内容
    NSData *data = UIImagePNGRepresentation(image);
    imageObject.imageData = data;
    // 多媒体数据对象，可以为WXImageObject，WXMusicObject，WXVideoObject，WXWebpageObject等。
    message.mediaObject = imageObject;
    SendMessageToWXReq *req = [[SendMessageToWXReq alloc] init];
    req.bText = NO;
    req.message = message;
    req.scene = scene;// 分享到朋友圈
    [WXApi sendReq:req];
}
@end
